/* 图片资料 ----start----*/
$( "button[title='Zoom In']" ).click(function(){
	$('#image').cropper('zoom', 0.1);
});
$( "button[title='Zoom Out']" ).click(function(){
	$('#image').cropper('zoom', -0.1);
});
$( "button[title='Move Left']" ).click(function(){
	$('#image').cropper("move", -10, 0);
});
$( "button[title='Move Right']" ).click(function(){
	$('#image').cropper("move", 10, 0);
});
$( "button[title='Move Up']" ).click(function(){
	$('#image').cropper("move", 0, -10);
});
$( "button[title='Move Down']" ).click(function(){
	$('#image').cropper("move", 0, 10);
});
$( "button[title='Rotate Left']" ).click(function(){
	$('#image').cropper("rotate", -45);
});
$( "button[title='Rotate Right']" ).click(function(){
	$('#image').cropper("rotate", 45);
});


var thisRukou;
var imgInfo = {};// 用来保存可修改图片信息
var unChangeImgInfo = {};// 用来保存不可可修改图片信息  
$(document).on("mouseover",".imagesmall", function(){
	var thisImgInfo;
	if(thisRukou == 1) {
		thisImgInfo = imgInfo;
	}else{
		thisImgInfo = unChangeImgInfo;
	}
	var imgInfoDetail = thisImgInfo[$(this).parent().find('.imagesmall').attr("value")];+
	$(this).attr('title',imgInfoDetail.beizhu);
});
$(document).on("click",".imagesmall,.img-item-name", function(){
	var thisImgInfo;
	var jiuzhenjl_txzl = $(this).parents(".jiuzhenjl-txzl");
	if(jiuzhenjl_txzl.length) {
		thisRukou = 2;
	}else {
		thisRukou = 1;
	}
	if(thisRukou == 1) {
		thisImgInfo = imgInfo;
		$("#detailimg .img-btns").css('display','block');
	}else{
		thisImgInfo = unChangeImgInfo;
		$("#detailimg .img-btns").css('display','none');
	}
	$(this).parent('.img-baogaod-item').find('.img-item-name').addClass('thisImgType');
	var thisTime = $(this).siblings('.img-item-time').text();
	var imgName = $(this).parent().find('.imagesmall').attr("value");
	var imgInfoDetail = thisImgInfo[$(this).parent().find('.imagesmall').attr("value")];
	$("#detailimg").attr("value", $(this).parent().find('.imagesmall').attr("value"));// 将标识放在当前div
	$("#detailimg img").attr("src", "");
	//设置显示属性
	//$.post(path+"/handelImg/getDatu",{imgName:imgName,isTmp:imgInfoDetail.tmp},function(result){
		$("#detailimg img").attr("src", path+"/handelImg/getDatuBack?imgName=" + imgName + "&isTmp=" + imgInfoDetail.tmp);
		
		$("#detailimg #imgLeixing").val(imgInfoDetail.leixing);
		if(imgInfoDetail.shangchuanrq) {
			$("#detailimg #imgdetaildate-datetimepicker1").val(imgInfoDetail.shangchuanrq);
		}else {
			$("#detailimg #imgdetaildate-datetimepicker1").val(thisTime);
		}
		
		$("#detailimg #imgBeizhu").val(imgInfoDetail.beizhu);
		var itemname = $(this).parent().attr('class');
		if(itemname == "img-baogaod-item" ){
			$('#detailimg .modal-title').text('报告单详情');
		} else {
			$('#detailimg .modal-title').text('病兆详情');
		}
		$("#detailimg").modal();
		setTimeout(function(){
			$('#image').cropper({
			  aspectRatio: 16 / 9,
			 	  viewMode: 0,
				  dragMode:'move', 
				  modal: false, // Show the black modal above the image and under the crop box
				  guides: true, // Show the dashed lines above the crop box.
				  center: false, // Show the center indicator above the crop box.
				  highlight: false, // Show the white modal above the crop box (highlight the crop box).
				  background: true, // Show the grid background of the container.
				  autoCrop: false, // Enable to crop the image automatically when initialize.
				  toggleDragModeOnDblclick: false, // Enable to toggle drag mode between "crop" and "move" when click twice on the cropper.
			      ready:function () {
			  	  
				 
					  /*crop: function(e) {
					    // Output the result data for cropping image.
					    console.log(e.x);
					    console.log(e.y);
					    console.log(e.width);
					    console.log(e.height);
					    console.log(e.rotate);
					    console.log(e.scaleX);
					    console.log(e.scaleY);
			 		  }*/
			 		  // $(this).cropper('crop');
			     }
			  
		});
		},500);
	//});
});

// 备注验证
$("#imgBeizhu").on('input',function(){
	var tt = $(this).val();
    if(tt.length>512){
		var text =tt.substring(0,511);
		$(this).val(text);
		notifyWarning("可输入最大长度为512个字符！");
	}else if(tt.length<512){
	}
});

// 点击事件 图片类型
$(document).on("click",".img-info-leixing .dropdown-menu li",function () {
	var bText = $(this).text();
	var bValue = $(this).attr('value');
	$(this).parent().siblings('.dropdown-toggle').val(bText);
	$(this).parent().css("display","none");
	return false;
});

$(document).on("click", ".img-info-leixing .dropdown-toggle", function(){
	var imgName = $(this).parents("#detailimg").attr("value");
	var imgInfoDetail = imgInfo[imgName];
	$.post(path+"/handelImg/getZSJiuzhentxzllxByfl",{fenlx:imgInfoDetail.fenlei},function(result){
		if(result.succsess) {
			var content = "";
			$(result.data).each(function(){
				content += '<li>'+this.mingcheng+'</li>';
			});
			$(".img-info-leixing .dropdown-menu").html(content);
		}
	});
});


// 图片详情点击保存
$("#imgBaocun").click(function(){
	// 将当前图片信息更新到imgInfo中
	//$('.thisImgType').html($("#imgLeixing").val());
	//$('.thisImgType').removeClass('thisImgType');
	var imgName = $(this).parents("#detailimg").attr("value");
	
	var imgInfoDetail = imgInfo[imgName];
	var isDataOkTime =$(this).parents('.modal-content').find('.img-info #imgdetaildate-datetimepicker1').val();
	var isDataOkType =$(this).parents('.modal-content').find('.img-info #imgLeixing').val();
	if(!isDataOkTime){
		notifyWarning("请填写日期！");
		return false;
	}
	if(!isDataOkType){
		notifyWarning("请填写类型！");
		return false;
	}
	// 将日期、类型、备注放入
	imgInfoDetail.leixing = $("#imgLeixing").val();
	imgInfoDetail.shangchuanrq = $("#imgdetaildate-datetimepicker1").val();
	imgInfoDetail.beizhu = $("#imgBeizhu").val();
	imgInfo[imgName] = imgInfoDetail;
	// 保存成功设置类型
	var bsName = imgName.substring(0,imgName.indexOf(".")); 
	$("[value="+bsName+"]").html(imgInfoDetail.leixing);
	$("[value="+bsName+"sj]").html(imgInfoDetail.shangchuanrq);
	$("#imgLeixing").val("");
	$("#imgBeizhu").val("");
	$("#detailimg").modal("hide");
});

// 图片点击取消
$("#imgQuxiao").click(function(){
	//$('.thisImgType').text($("#imgLeixing").val());
	$('.thisImgType').removeClass('thisImgType');
	$("#detailimg").modal("hide");
});

//图片点击删除
$("#imgShanchu").click(function(){
	var imgName = $(this).parents("#detailimg").attr("value");
	var imgInfoDetail = imgInfo[imgName];
	imgInfoDetail.shanchubz = true;
	imgInfo[imgName] = imgInfoDetail;
	rendererImg(".img-hasimginof", 1);
	$("#detailimg").modal("hide");
});
// 改变类型事件 TODO该方式有问题
//$('#detailimg .modal-header button').click(function(){
//	$('.thisImgType').text($("#imgLeixing").val());
//	$('.thisImgType').removeClass('thisImgType');
//});
var jiuzhenjlid = null;
// rukou=1从常规门诊等可修改的入口来
// rukou=2从就诊记录等不可修改的入口来
function getTuxiangzl(keyArry, jiuzhenjlid, shangcengmc,rukou) {
	unChangeImgInfo={}; // 该只读数据应该调用一次清空一次
	thisRukou = rukou;
	jiuzhenjlid = jiuzhenjlid;
	// 获取到以保存的图像资料
	$.post(path + "/handelImg/getTuxiangzl", {
		key : JSON.stringify(keyArry),
		jiuzhenjlId : jiuzhenjlid
	}, function(result) {
		$(result.data.baogaods).each(function() {
			var imgName = this.imgName;
			this.shangchuanrq = common_util.formatDataToRiqi(this.shangchuanrq);
			if(rukou == 1){
				if(imgInfo[imgName] == undefined)
					imgInfo[imgName] = this;
			}else{
				unChangeImgInfo[imgName] = this;
			}
		});
		var bingzhaoContent = '';
		$(result.data.bingzhaos).each(function() {
			var imgName = this.imgName;
			this.shangchuanrq = common_util.formatDataToRiqi(this.shangchuanrq);
			if(rukou == 1){
				if(imgInfo[imgName] == undefined)
					imgInfo[imgName] = this;
			}else{
				unChangeImgInfo[imgName] = this;
			}
		});
		rendererImg(shangcengmc,rukou);
	});
}

// 渲染图片到页面
function rendererImg(shangcengmc,rukou) {
	var baogaodContent = '';
	var bingzhaoContent = '';
	var rendererImgInfo;
	var commonContent = '';
	if(rukou == 1){
		rendererImgInfo = imgInfo;
		commonContent +='<div id="key" style="display: none"></div>'+
						'<div class="img-all-item" style="display:inline-block;width:860px;position: relative;vertical-align: middle;margin-left: 80px;margin-top: 15px;border: solid 1px #00b472;">'+
							'<div class="jzjl-img-baogaod">'+	
								'<div class="img-baogaod-header" >报告单</div>'+	
								'<div class="img-baogaod-main" id="imgBaogaodMain">'+		
								'</div>'+		
							'</div>'+	
							'<div class="jzjl-img-bingzhao">'+	
								'<div class="img-bingzhao-header" >病兆</div>'+		
								'<div class="img-bingzhao-main" id="imgBingzhaoMain">'+	
								'</div>'+		
							'</div>'+	
						'</div>';
		$(shangcengmc).html(commonContent);
		$("#imgdetaildate-datetimepicker1").attr("disabled",false).css({"backgroundColor":"#fff","cursor": "auto;"});
		$("#imgLeixing").attr("readonly",false).css("backgroundColor","#fff");
		$("#imgBeizhu").attr("disabled",false).css("backgroundColor","#fff");
	} else {
		rendererImgInfo = unChangeImgInfo;
		commonContent +='<div id="key" style="display: none"></div>'+
						'<span class="sdlm-nav" style="vertical-align: top; padding-top: 10px;">图像资料</span>'+
						'<div  class="img-all-item" style="display:inline-block;width:860px;position: relative;vertical-align: middle;margin-top: 15px;border: solid 1px #00b472;">'+
							'<div class="jzjl-img-baogaod">'+	
								'<div class="img-baogaod-header" >报告单</div>'+	
								'<div class="img-baogaod-main" id="imgBaogaodMain">'+		
								'</div>'+		
							'</div>'+	
							'<div class="jzjl-img-bingzhao">'+	
								'<div class="img-bingzhao-header" >病兆</div>'+		
								'<div class="img-bingzhao-main" id="imgBingzhaoMain">'+	
								'</div>'+		
							'</div>'+	
						'</div>';
		$(shangcengmc).html(commonContent);
		$("#imgdetaildate-datetimepicker1").attr("disabled","disabled").css({"backgroundColor":"#eee","cursor": "auto;"});
		$("#imgLeixing").attr("readonly","readonly").css("backgroundColor","#eee");
		$("#imgBeizhu").attr("disabled","disabled").css("backgroundColor","#eee");
	}
	for(var key in rendererImgInfo) {
		var img = rendererImgInfo[key];
		var leixing = img.leixing;
		var shangchuanrq = img.shangchuanrq;
		if(leixing == "" || leixing == null) {
			leixing = "请在详情中确认类型";
			shangchuanrq = common_util.formatDataToRiqi(new Date().getTime());
		}
		// 渲染未删除的
		if(!img.shanchubz) {
			if(img.fenlei == 1) {//报告单
				var bsName = img.imgName;
				bsName = bsName.substring(0,bsName.indexOf(".")); 
				baogaodContent += '<div class="img-baogaod-item">'+
				'<img class="imagesmall" data-toggle="tooltip" data-placement="bottom" title="hello world" value="'+img.imgName+'" title="'+ (img.beizhu || '' )+'" src="'+img.miniImgStr+'">'+
				'<div class="img-item-time"  value="'+bsName+'sj">'+shangchuanrq+'</div>';
				if(leixing == "请在详情中确认类型"){
					baogaodContent += '<div class="img-item-name" value="'+bsName+'"><span style="color:red">'+leixing+'</span></div>';
				}else {
					baogaodContent += '<div class="img-item-name" value="'+bsName+'">'+leixing+'</div>';
				}
					baogaodContent += '</div>';
			}else{
				var bsName = img.imgName;
				bsName = bsName.substring(0,bsName.indexOf(".")); 
				bingzhaoContent += '<div class="img-bingzhao-item" >'+
					'<img class="imagesmall" data-toggle="tooltip" data-placement="bottom" title="hello world" value="'+img.imgName+'" title="'+ (img.beizhu || '' )+'" src="'+img.miniImgStr+'">'+
					'<div class="img-item-time"  value="'+bsName+'sj">'+shangchuanrq+'</div>';
					if(leixing == "请在详情中确认类型"){
						bingzhaoContent += '<div class="img-item-name" value="'+bsName+'"><span style="color:red">'+leixing+'</span></div>';
					}else {
						bingzhaoContent += '<div class="img-item-name" value="'+bsName+'">'+leixing+'</div>';
					}
					bingzhaoContent += '</div>';
			}
		}
	}
	if(baogaodContent != "" || bingzhaoContent != "") {
		if(baogaodContent == ""){
			baogaodContent = '<div style="color: #CCCCCC;text-align: center;margin: 80px 0px;">当前无图像</div>';
		}
		if(bingzhaoContent == ""){
			bingzhaoContent = '<div style="color: #CCCCCC;text-align: center;margin: 80px 0px;">当前无图像</div>';
		}
		$(shangcengmc + " #imgBaogaodMain").html(baogaodContent);
		$(shangcengmc + " #imgBingzhaoMain").html(bingzhaoContent);
		$(shangcengmc).css('display','block');
	}else{
		$(shangcengmc).css('display','none');
	}
}

common_util.dateTimePickerOne("#imgdetaildate-datetimepicker1");

/* 图片资料 ----end----*/